﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using mitasoft.AdsShedule.Infrastructure;

namespace mitasoft.AdsShedule.Forms
{
    public partial class ManageVoicesAssoc : Infrastructure.BaseForm
    {
        #region fields
        private BindingSource bsAdVoices;
        private BindingSource bsClients;
        private BindingSource bsVoiceClientAssoc; 
        #endregion

        #region ctor
        public ManageVoicesAssoc()
        {
            InitializeComponent();
            this.Load += new EventHandler(ManageVoices_Load);
        }
        #endregion

        #region override
        public override void LoadData()
        {
            bsClients = new BindingSource(Business.BusinessAccess.Instance.DataSet, "Client");
            bsAdVoices = new BindingSource(Business.BusinessAccess.Instance.DataSet, "AdVoice");
            bsVoiceClientAssoc = new BindingSource(Business.BusinessAccess.Instance.DataSet, "VoiceClientAssoc");
        }
        public override void BindDataToControls()
        {
            ddlClients.DataSource = bsClients;
            ddlClients.DisplayMember = "Name";
            ddlClients.ValueMember = "ID";

            ddlVoice.DataSource = bsAdVoices;
            ddlVoice.DisplayMember = "Description";
            ddlVoice.ValueMember = "ID";

            dataGridVoices.AutoGenerateColumns = false;
            dataGridVoices.DataSource = bsVoiceClientAssoc;

            ddlVoice.DataBindings.Clear();
            ddlVoice.DataBindings.Add(new Binding("SelectedValue", bsVoiceClientAssoc, "IDAdVoice"));

            ddlClients.DataBindings.Clear();
            ddlClients.DataBindings.Add(new Binding("SelectedValue", bsVoiceClientAssoc, "IDClient"));
        } 
        #endregion

        #region handlers
        private void ManageVoices_Load(object sender, EventArgs e)
        {
            AfterSave();
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            BeforeSave();

            ErrorCheck.DoCheck(() =>
            {
                bsVoiceClientAssoc.AddNew();
            });
        }
        private void btnSalveaza_Click(object sender, EventArgs e)
        {
            ErrorCheck.DoCheck(() =>
            {
                var voiceRow = ((mitasoft.AdsShedule.Business.AdDS.VoiceClientAssocRow)(((System.Data.DataRowView)(bsVoiceClientAssoc.Current)).Row));

                voiceRow.IDAdVoice = Convert.ToInt32(ddlVoice.SelectedValue);
                voiceRow.IDClient = Convert.ToInt32(ddlClients.SelectedValue);
                voiceRow.Description = string.Format("{0} - {1}", ddlClients.Text, ddlVoice.Text);

                Business.BusinessAccess.Instance.DataSet.VoiceClientAssoc.AddVoiceClientAssocRow(voiceRow);
                dataGridVoices.Refresh();
            });

            AfterSave();

        }
        private void btnDelete_Click(object sender, EventArgs e)
        {
            ErrorCheck.DoCheck(() =>
            {
                bsVoiceClientAssoc.RemoveCurrent();
            });
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        } 
        #endregion

        #region private methods
        private void BeforeSave()
        {
            ddlClients.Enabled = true;
            ddlVoice.Enabled = true;
        }
        private void AfterSave()
        {
            ddlClients.Enabled = false;
            ddlVoice.Enabled = false;
        } 
        #endregion

    }
}
